<?php
@include("connect.php");
//分页
$page = $_GET['page'];
$showSize = $_GET['showSize'];




// $kw = "电视";

$kw = $_GET['kw'];  //模糊查询
// $kw = '手机';

$sortName = $_GET['sortName']; //按什么排序 类型  手机 

$sortType = $_GET['sortType']; //升序 降序

$start = ($page - 1) * $showSize;

$obj = [];


if ($sortName) {
    $sql1 = "select count(*) pageAll  from goodlist where type='$sortName' order by price $sortType";
    $sql2 = "select * from goodlist where type = '$sortName' order by  price  $sortType limit $start , $showSize";
} else if ($kw) {
    $sql1 = "select count(*) pageAll from goodlist where title like '%$kw%' order by  price  $sortType ";
    $sql2 = "select * from goodlist where title like '%$kw%' order by  price  $sortName limit $start , $showSize ";
} else if ($sortType) {
    $sql1 = "select count(*) pageAll from goodlist order by  price  $sortType";
    $sql2 = "select * from goodlist order by  price  $sortType limit $start , $showSize";
} else {
    $sql1 = "select count(*) pageAll from goodlist ";
    $sql2 = "select * from goodlist limit $start , $showSize";
}








// 默认只会转第一条数据
$res = mysqli_query($conn, $sql1);
$arr = mysqli_fetch_array($res);
$pageAll = $arr['pageAll'];


$res = mysqli_query($conn, $sql2);
$list = [];

while ($data = mysqli_fetch_array($res)) {
    $temp = [];
    $temp['id'] = $data['id'];
    $temp['title'] = $data['title'];
    $temp['img'] = $data['img'];
    $temp['price'] = $data['price'];
    $temp['type'] = $data['type'];
    array_push($list, $temp);
}

if ($list) {
    $obj['status'] = true;
    $obj['msg'] = '数据请求成功';
    $obj['data'] = $list;
    $obj['pageAll'] = $pageAll;
} else {
    $obj['status'] = false;
    $obj['msg'] = '服务器错误';
    $obj['data'] = $list;
}

echo (json_encode($obj, JSON_UNESCAPED_UNICODE));
